//
//  WBOAuthViewModel.m
//  sinaWeibo
//
//  Created by 李江波 on 2016/11/16.
//  Copyright © 2016年 lijiangbo. All rights reserved.
//

#import "WBOAuthViewModel.h"
#import "LEEHTTPManager.h"

static id _instance;
@implementation WBOAuthViewModel


+(instancetype)shared{

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _instance = [[self alloc]init];
    });
    
    return _instance;
}

-(instancetype)init{

    self = [super init];
    
    self.userModel = [self getUserInfo];
    
    return self;
}


-(BOOL)isLogin{

    if (self.userModel.access_token != nil && ![self isExpires]) {
        
        return YES;
    }
    return NO;
}

-(BOOL)isExpires{

    if ([self.userModel.expires_date compare:[NSDate new]] == NSOrderedAscending) {
        
        return YES;
    }
    
    return NO;
}


#pragma mark : - 拿到令牌access_token

-(void)getAccess_token:(NSString *)code completion:(void(^)(BOOL))completion{
    
    NSString *urlStr = @"https://api.weibo.com/oauth2/access_token";
    
    NSDictionary *parameters = @{@"client_id" : client_id,
                                 @"client_secret" : client_secret,
                                 @"grant_type" : @"authorization_code",
                                 @"code" : code,
                                 @"redirect_uri" : redirect_uri};
    
    [[LEEHTTPManager share] request:POST UrlString:urlStr parameters:parameters finshed:^(id responseObject, NSError *error) {
        
        if (error != nil) {
            
            completion(false);
//            [SVProgressHUD showErrorWithStatus:@"网络有问题哦，请重试"];
            return ;
        }
        
        NSLog(@"%@",responseObject);
        [self getUserInfo:responseObject completion:completion];
        
    }];
    
}


#pragma mark : - 得到用户数据
-(void)getUserInfo:(NSDictionary *)dict completion:(void(^)(BOOL))completion {
    
    NSString *uid = dict[@"uid"];
    NSString *access_token = dict[@"access_token"];
    
    NSString *urlStr = @"https://api.weibo.com/2/users/show.json";
    NSDictionary *parameters = @{@"access_token" : access_token,
                                 @"uid" : uid};
    
    [[LEEHTTPManager share] request:GET UrlString:urlStr parameters:parameters finshed:^(NSDictionary* responseObject, NSError *error) {
        
        if (error != nil) {
            
            completion(false);
            return ;
        }
        
        
        NSMutableDictionary *mutableDict = responseObject.mutableCopy;
        
        [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            
            [mutableDict setValue:obj forKey:key];
            
        }];
        
        
        WBUserInfoModel *model = [[WBUserInfoModel alloc]initWithUserInfoModel:mutableDict];
        
        NSLog(@"%@",model);
        [self saveUserModel:model];
        
        //解决首次登录回到tabview 问题
        self.userModel = model;
        completion(YES);
        
    }];
    
}

#pragma mark : - 存档

-(void)saveUserModel:(WBUserInfoModel *)model {
    
    NSLog(@"%@",documentPath);
    [NSKeyedArchiver archiveRootObject:model toFile:documentPath];
    
}



#pragma mark : - 解档
-(WBUserInfoModel *)getUserInfo{
    
    NSLog(@"%@",documentPath);
    WBUserInfoModel *model = (WBUserInfoModel*)[NSKeyedUnarchiver unarchiveObjectWithFile:documentPath];
    return model;
}


@end
